package com.mengxuegu.springboot.component;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.LocaleResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;
import java.util.Set;

public class MyLocaleResolver implements LocaleResolver {
    Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    public Locale resolveLocale(HttpServletRequest httpServletRequest) {
        String l = httpServletRequest.getParameter("l");
        logger.info("解析区域信息"+l);

        //获取操作系统区域信息
//        Locale aDefault = Locale.getDefault();
        //获取浏览器区域信息
        Locale locale = httpServletRequest.getLocale();

        if (!StringUtils.isEmpty(l)){
            String[] s = l.split("_");
            locale = new Locale(s[0], s[1]);
        }
        logger.info("最终采用的区域信息: "+ locale.getLanguage() +"_"+locale.getCountry());

        return locale;
    }

    @Override
    public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {

    }
}
